import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * 文件相关
 * @creationDate 2019/12/25
 */
import request from "../../utils/request";
import { getEnvConfig } from "../../utils/iocUtils";
import { getCurrentOrganizationId, isTenantRoleLevel, getResponse } from "../../utils/utils";

var _getEnvConfig = getEnvConfig(),
    CPAAS_PLATFORM = _getEnvConfig.CPAAS_PLATFORM;
/**
 * 保存
 * @export
 * @param {object} params 传递参数
 */


export function saveGanttTaskData(_x) {
  return _saveGanttTaskData.apply(this, arguments);
}
/**
 * 保存
 * @export
 * @param {object} params 传递参数
 */

function _saveGanttTaskData() {
  _saveGanttTaskData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(gantt) {
    var organizationId, apiPrefix, arr;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            organizationId = getCurrentOrganizationId();
            apiPrefix = isTenantRoleLevel() ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId) : "".concat(CPAAS_PLATFORM, "/v1");
            arr = gantt.$data.tasksStore.getItems().map(function (item) {
              return {
                ganttId: gantt.serverList('ganttId')[0],
                taskId: item.objectVersionNumber ? item.id : undefined,
                taskName: item.text,
                orderSeq: item.order,
                startDate: "".concat(item.start_date.getFullYear(), "-").concat(item.start_date.getMonth() + 1, "-").concat(item.start_date.getDate(), " 00:00:00"),
                endDate: "".concat(item.end_date.getFullYear(), "-").concat(item.end_date.getMonth() + 1, "-").concat(item.end_date.getDate(), " 00:00:00"),
                parentId: item.parent || null,
                taskType: item.type || 'task',
                tenantId: item.tenantId || organizationId,
                objectVersionNumber: item.objectVersionNumber,
                _token: item._token,
                duration: item.duration,
                priority: item.priority,
                progress: item.progress.toFixed(2),
                taskRender: item.render && item.render[0]
              };
            });
            return _context.abrupt("return", request("".concat(apiPrefix, "/gantt-tasks/batch"), {
              method: 'POST',
              body: arr
            }));

          case 4:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _saveGanttTaskData.apply(this, arguments);
}

export function getGanttTaskData(_x2) {
  return _getGanttTaskData.apply(this, arguments);
}
/**
 * 保存
 * @export
 * @param {object} params 传递参数
 */

function _getGanttTaskData() {
  _getGanttTaskData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(gantt) {
    var organizationId, apiPrefix, res, data;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            organizationId = getCurrentOrganizationId();
            apiPrefix = isTenantRoleLevel() ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId) : "".concat(CPAAS_PLATFORM, "/v1");
            _context2.next = 4;
            return request("".concat(apiPrefix, "/gantt-tasks"), {
              method: 'GET',
              query: {
                tenantId: organizationId,
                ganttId: gantt.serverList('ganttId')[0]
              }
            });

          case 4:
            res = _context2.sent;
            data = getResponse(res, function (e) {
              return e;
            });
            return _context2.abrupt("return", formatGanttTaskData(data) || []);

          case 7:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _getGanttTaskData.apply(this, arguments);
}

export function deleteGanttTaskData(_x3) {
  return _deleteGanttTaskData.apply(this, arguments);
}
/**
 * 保存
 * @export
 * @param {object} params 传递参数
 */

function _deleteGanttTaskData() {
  _deleteGanttTaskData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(task) {
    var organizationId, apiPrefix, obj, res;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            organizationId = getCurrentOrganizationId();
            apiPrefix = isTenantRoleLevel() ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId) : "".concat(CPAAS_PLATFORM, "/v1");
            obj = task;
            _context3.next = 5;
            return request("".concat(apiPrefix, "/gantt-tasks"), {
              method: 'DELETE',
              body: obj
            });

          case 5:
            res = _context3.sent;
            return _context3.abrupt("return", getResponse(res, function (e) {
              return e;
            }));

          case 7:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _deleteGanttTaskData.apply(this, arguments);
}

export function saveGanttLinkData(_x4) {
  return _saveGanttLinkData.apply(this, arguments);
}
/**
 * 保存
 * @export
 * @param {object} params 传递参数
 */

function _saveGanttLinkData() {
  _saveGanttLinkData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(gantt) {
    var organizationId, apiPrefix, arr;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            organizationId = getCurrentOrganizationId();
            apiPrefix = isTenantRoleLevel() ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId) : "".concat(CPAAS_PLATFORM, "/v1");
            arr = gantt.$data.linksStore.getItems().map(function (item) {
              return {
                ganttId: gantt.serverList('ganttId')[0],
                linkType: item.type,
                taskFromId: item.source,
                taskTargetId: item.target,
                taskLinkId: item.objectVersionNumber ? item.id : undefined,
                tenantId: item.tenantId || organizationId,
                objectVersionNumber: item.objectVersionNumber,
                _token: item._token
              };
            });
            return _context4.abrupt("return", request("".concat(apiPrefix, "/gantt-task-links"), {
              method: 'POST',
              body: arr
            }));

          case 4:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _saveGanttLinkData.apply(this, arguments);
}

export function getGanttLinkData(_x5) {
  return _getGanttLinkData.apply(this, arguments);
}
/**
 * 保存
 * @export
 * @param {object} params 传递参数
 */

function _getGanttLinkData() {
  _getGanttLinkData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(gantt) {
    var organizationId, apiPrefix, res, data;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            organizationId = getCurrentOrganizationId();
            apiPrefix = isTenantRoleLevel() ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId) : "".concat(CPAAS_PLATFORM, "/v1");
            _context5.next = 4;
            return request("".concat(apiPrefix, "/gantt-task-links/").concat(gantt.serverList('ganttId')[0]), {
              method: 'GET'
            });

          case 4:
            res = _context5.sent;
            data = getResponse(res, function (e) {
              return e;
            });
            return _context5.abrupt("return", formatGanttLinkData(data) || []);

          case 7:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _getGanttLinkData.apply(this, arguments);
}

export function deleteGanttLinkData(_x6) {
  return _deleteGanttLinkData.apply(this, arguments);
}
/**
 * 获取数据
 * @export
 * @param {object} params 传递参数
 */

function _deleteGanttLinkData() {
  _deleteGanttLinkData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(link) {
    var organizationId, apiPrefix, res;
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            organizationId = getCurrentOrganizationId();
            apiPrefix = isTenantRoleLevel() ? "".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId) : "".concat(CPAAS_PLATFORM, "/v1");
            _context6.next = 4;
            return request("".concat(apiPrefix, "/gantt-task-links"), {
              method: 'DELETE',
              body: link
            });

          case 4:
            res = _context6.sent;
            return _context6.abrupt("return", getResponse(res, function (e) {
              return e;
            }));

          case 6:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _deleteGanttLinkData.apply(this, arguments);
}

export function formatGanttTaskData() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var arr = data.map(function (item) {
    var temp = item;
    return _objectSpread(_objectSpread({}, temp), {}, {
      id: temp.taskId,
      text: temp.taskName,
      order: temp.orderSeq,
      start_date: temp.startDate.substr(0, 10),
      end_date: temp.endDate.substr(0, 10),
      parent: temp.parentId || null,
      render: [temp.taskRender]
    });
  });
  return arr;
}
/**
 * 获取数据
 * @export
 * @param {object} params 传递参数
 */

export function formatGanttLinkData() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var arr = data.map(function (item) {
    var temp = item;
    return _objectSpread(_objectSpread({}, temp), {}, {
      type: temp.linkType,
      source: temp.taskFromId,
      target: temp.taskTargetId,
      id: temp.taskLinkId
    });
  });
  return arr;
}